package cn.wolfcode.springcloud.service.impl;

import cn.wolfcode.springcloud.domain.Order;
import cn.wolfcode.springcloud.domain.Product;
import cn.wolfcode.springcloud.feign.ProductFeignApi;
import cn.wolfcode.springcloud.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {

    //因为进行负载均衡调用, 此时需要用的是 Eureka 中 product-server 服务名
    //因为Eureka 中中可以通过服务名 获取 product-server 所有服务集群的ip 与 端口
   /* public static final String PRODUCT_URL = "http://PRODUCT-SERVER";

   @Autowired
   private RestTemplate template;*/

   @Autowired
   private ProductFeignApi productFeignApi;

    @Override
    public Order save(Long userId, Long productId) {

        System.out.println("OrderServiceImpl..save..");

        //记录调用日志
        log.info("OrderServiceImpl.save....");
        Product product = productFeignApi.get(productId);
        // Product product = template.getForObject(PRODUCT_URL + "/products/get/" + productId, Product.class);

        //Product product = null; //模拟远程获取 product

        Order order = new Order();
        order.setOrderNo(UUID.randomUUID().toString().replaceAll("-", ""));
        order.setCreateTime(new Date());
        order.setProductName(product.getName());
        order.setProductPrice(product.getPrice());
        order.setUserId(userId);
        return order;
    }
}
